package com.young.shop.admin.service.impl;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.young.shop.admin.dao.GoodsMapper;
import com.young.shop.admin.service.GoodsService;
import com.young.shop.pojo.entity.Goods;
import com.young.shop.pojo.entity.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sound.midi.Soundbank;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	GoodsMapper goodsMapper;


	//商品上架
	@Override
	public int checkShelvesGoods(Integer id) {

		//判断id 是否为空
		if (id == null) {
			throw new RuntimeException("无法查询该商品信息");
		}
		//获取当前时间
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateFormat = simpleDateFormat.format(new Date());

		return goodsMapper.updateGoodsById(id, 1, dateFormat);

	}


	//商品下架
	@Override
	public int checkSoldOutGoods(Integer id) {

		//判断id 是否为空
		if (id == null) {
			throw new RuntimeException("无法查询该商品信息");
		}
		//获取当前时间
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateFormat = simpleDateFormat.format(new Date());

		return goodsMapper.updateGoodsById(id, 0, dateFormat);
	}


	//service层查询某个特定页面的数据给web层
	@Override
	public Page findGoods(Integer status,
						  Integer currentPage,
						  Integer pageSize,
								 String type) {

		//limit第一个参数:从数据库中的哪里开始查,索引是从0开始的
		List<Goods> goods = goodsMapper.selectGoods(status, (currentPage - 1) * pageSize, pageSize, type);

		//获取总共有多少条数据
		Long totalCount = (Long) goodsMapper.findTotalCount(type,status);

		//封装Page参数
		Page page = new Page();
		page.setTotalCount(totalCount.intValue());
		page.setCurrentPage(currentPage);
		page.setPageSize(pageSize);
		page.setList(goods);
		//向上取整,计算总页数,不要忘了乘以1.0,否则会少一页数据
		int totalPage = (int) Math.ceil(totalCount * 1.0 / pageSize);
		page.setTotalPage(totalPage);

		//把数据给servlet
		return page;
	}


	@Override
	public int addGoods(Goods goods) {

		System.out.println(goods);

		int insertGoods = goodsMapper.insertGoods(goods);

		System.out.println(insertGoods);
		return insertGoods;
	}


	@Override
	public int deleteGoods(Integer id) {

		//判断id 是否为空
		if (id == null) {
			throw new RuntimeException("无法查询该商品信息");
		}

		return goodsMapper.deleteGoodsById(id);

	}










	/*@Override
	public List<Goods> getAllGoods() {
		//获取所有商品数据
		return goodsMapper.selectAllGoods();
	}*/




	/*//封装Page数据
	@Override
	public Page findPageInfo(Integer status, Integer currentPage, Integer pageSize, String type) {

		//type为空分页查询所有数据，type不为空根据类型查询数据
		List<Goods> goods = goodsMapper.selectGoods(status, (currentPage - 1) * pageSize, pageSize, type);

		//获取总共有多少条数据
		Long totalCount = (Long) goodsMapper.selectTotalCount();

		//封装Page参数
		Page page = new Page();
		page.setTotalCount(totalCount.intValue());
		page.setCurrentPage(currentPage);
		page.setPageSize(pageSize);
		page.setList(goods);
		//向上取整,计算总页数,不要忘了乘以1.0,否则会少一页数据
		int totalPage = (int) Math.ceil(totalCount * 1.0 / pageSize);
		page.setTotalPage(totalPage);

		//把数据给servlet
		return page;
	}*/


	/*@Override
	public List<Goods> selectGoodsListByType(String type) {
		//判断type是否为空，为空赋值
		if (type == null || "".equals(type.trim())) {
			//获取所有商品数据
			return goodsMapper.selectAllGoods();
		}
		//根据商品类型查询商品
		List<Goods> goodsByType = goodsMapper.selectGoodsByType(type);
		return goodsByType;
	}*/






/*
	@Override
	public Page goodsByStatus(Integer status, Integer currentPage, Integer pageSize) {
		//status 是否为空
		if (status == 2) {
			throw new RuntimeException("无法查询该商品信息");
		}
		//status=1,查找在售商品
		//status=0,查找下架商品
		List<Goods> goods = goodsMapper.selectGoodsByStatus(status, (currentPage - 1) * pageSize, pageSize);

		//获取总共有多少条数据
		Long totalCount = 0L;
		if (status == 1) {
			totalCount = (Long) goodsMapper.selectOnSaleGoodsCount();
		}if (status == 0){
			totalCount = (Long) goodsMapper.selectUnSaleGoodsCount();
		}

		//封装Page参数
		Page page = new Page();
		page.setTotalCount(totalCount.intValue());
		page.setCurrentPage(currentPage);
		page.setPageSize(pageSize);
		page.setList(goods);
		//向上取整,计算总页数,不要忘了乘以1.0,否则会少一页数据
		int totalPage = (int) Math.ceil(totalCount * 1.0 / pageSize);
		page.setTotalPage(totalPage);

		//把数据给servlet

		return page;

	}*/
/*

	@Override
	public List<Goods> goodsByStatus1(Integer status) {


		//status 是否为空
		if (status == null) {
			throw new RuntimeException("无法查询该商品信息");
		}
		List<Goods> goodsList = goodsMapper.goodsByStatus(status);
		return goodsList;
	}
*/



}
